package com.xijia.wuyouwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.CrashData;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/26 0026.
 */

public class CrashActivity extends BaseActivity implements View.OnClickListener{

    private ImageView crash_back;

    private LinearLayout crash_type_ll;
    private ImageView crash_type_img;
    private TextView crash_type_tv;
    private EditText crash_money_et;

    private TextView crash_money_surplusMOney;
    private TextView crash_money_all,crash_money_sure;

    private String amount,money;

    private int style;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crash);
        initView();
    }

    private void initView(){
        crash_back = (ImageView) findViewById(R.id.crash_back);
        crash_back.setOnClickListener(this);
        crash_type_ll = (LinearLayout) findViewById(R.id.crash_type_ll);
        crash_type_ll.setOnClickListener(this);
        crash_type_img = (ImageView) findViewById(R.id.crash_type_img);
        crash_type_tv = (TextView) findViewById(R.id.crash_type_tv);
        crash_money_et = (EditText) findViewById(R.id.crash_money_et);

        crash_money_surplusMOney = (TextView) findViewById(R.id.crash_money_surplusMOney);
        crash_money_all = (TextView) findViewById(R.id.crash_money_all);
        crash_money_all.setOnClickListener(this);
        crash_money_sure = (TextView) findViewById(R.id.crash_money_sure);
        crash_money_sure.setOnClickListener(this);

        money = getIntent().getStringExtra("money");
        crash_money_surplusMOney.setText("可用余额："+money);
    }

    /**
     * 提现
     */
    private void crash(){
        amount = crash_money_et.getText().toString();
        if (amount.isEmpty()){
            showToast("请输入提现金额");
            return;
        }
        if (Double.parseDouble(amount)<=1){
            showToast("提现不得金额小于1");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("amount",amount);
        showProgressDialog(true);
        XUtil.Post(UrlConfig.CRASH_MONEY_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("提现result="+result);
                CrashData data = JSONObject.parseObject(result,CrashData.class);
                if (data.getCode()==200){
                    //Log.e("info","剩余金额="+data.getData().getLeft());
                    MyApp.userData.getData().setLeft_money(data.getData().getLeft());
                    //MyApp.isMoney = true;
                    CrashActivity.this.setResult(Constants.RESULT_DATA);
                    CrashActivity.this.finish();
                }
                showToast(data.getMsg());
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                MyLogger.wLog().e("提现失败");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.crash_back:
                finish();
                break;
            case R.id.crash_type_ll://选择支付方式
                jumpToPage(BankSelectActivity.class,true, Constants.REQUEST_DATA);
                break;
            case R.id.crash_money_all:
                crash_money_et.setText("");
                crash_money_et.setText(MyApp.userData.getData().getLeft_money());
                break;
            case R.id.crash_money_sure://提现
                if (MyApp.userData.getData().getCertification_state().equals("0")){
                    jumpToPage(AttestationActivity.class);
                    return;
                }
                if (MyApp.userData.getData().getAccount_info().getAlipay().getState()==0){
                    jumpToPage(AplipayBingdingActivity.class);
                    return;
                }
                crash();
                break;

        }
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constants.REQUEST_DATA&&resultCode==Constants.REQUEST_DATA){
            if (data.getStringExtra("type").equals("1")){
                style = 1;
                crash_type_img.setImageResource(R.mipmap.ic_wallet_tixian_alipay);
                crash_type_tv.setText("支付宝提现");
            }else if (data.getStringExtra("type").equals("2")){
                style = 2;
                crash_type_img.setImageResource(R.mipmap.ic_wallet_tixian_weixin);
                crash_type_tv.setText("微信提现");
            }
        }
    }
}
